package com.hoshiicloud.user.controller.neo4j;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.user.neo4j.node.RNode;
import com.hoshiicloud.user.neo4j.node.Tenant;
import com.hoshiicloud.user.neo4j.node.relation.REdge;
import com.hoshiicloud.user.util.neo4j.Neo4jUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.neo4j.driver.v1.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/neo4j")
@Api(value = "Neo4j接口", tags = "Neo4j接口")
public class Neo4jController {


	@Autowired
	private Session session;


	final static ObjectMapper mapper = new ObjectMapper();

	static {
		mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
	}


	@RequestMapping(value = "/create/tenant", method = RequestMethod.POST)
	@ApiOperation("新增租户")
	public Response createTenant(@RequestBody RNode rNode) {
		try {

			Neo4jUtil.createTenant(mapper, session, rNode);

		} catch (Exception e) {
			return Response.failed(e.getMessage());

		}
		return Response.success("创建成功");

	}

	@RequestMapping(value = "/create/platform", method = RequestMethod.POST)
	@ApiOperation("新增平台")
	public Response createPlatform(@RequestBody RNode rNode) {
		try {

			Neo4jUtil.createPlatform(mapper, session, rNode);

		} catch (Exception e) {
			return Response.failed(e.getMessage());

		}
		return Response.success("创建成功");


	}

	@RequestMapping(value = "/create/store", method = RequestMethod.POST)
	@ApiOperation("新增店铺")
	public Response createStore(@RequestBody RNode rNode) {
		try {

			Neo4jUtil.createStore(mapper, session, rNode);

		} catch (Exception e) {
			return Response.failed(e.getMessage());

		}
		return Response.success("创建成功");
	}

	@RequestMapping(value = "/create/member", method = RequestMethod.POST)
	@ApiOperation("新增注册会员")
	public Response createMember(@RequestBody RNode rNode) {
		try {
			Neo4jUtil.createMember(mapper, session, rNode);
		} catch (Exception e) {
			return Response.failed(e.getMessage());
		}
		return Response.success("创建成功");
	}

	@RequestMapping(value = "/create/consumer", method = RequestMethod.POST)
	@ApiOperation("新增平台会员")
	public Response createConsumer(@RequestBody RNode rNode) {
		try {
			Neo4jUtil.createConsumer(mapper, session, rNode);
		} catch (Exception e) {
			return Response.failed(e.getMessage());
		}
		return Response.success("创建成功");
	}

	@RequestMapping(value = "/create/sysPlat", method = RequestMethod.POST)
	@ApiOperation("新增SaaS平台")
	public Response createSysPlat(@RequestBody RNode rNode) {
		try {
			Neo4jUtil.createSysPlat(mapper, session, rNode);
		} catch (Exception e) {
			return Response.failed(e.getMessage());
		}
		return Response.success("创建成功");
	}

	@RequestMapping(value = "/create/sysOrg", method = RequestMethod.POST)
	@ApiOperation("新增SaaS部门")
	public Response createSysOrg(@RequestBody RNode rNode) {
		try {
			Neo4jUtil.createSysOrg(mapper, session, rNode);
		} catch (Exception e) {
			return Response.failed(e.getMessage());
		}
		return Response.success("创建成功");
	}

	@RequestMapping(value = "/create/sysUser", method = RequestMethod.POST)
	@ApiOperation("新增SaaS部门用户")
	public Response createSysUser(@RequestBody RNode rNode) {
		try {
			Neo4jUtil.createSysUser(mapper, session, rNode);
		} catch (Exception e) {
			return Response.failed(e.getMessage());
		}
		return Response.success("创建成功");
	}

	@RequestMapping(value = "/create/comDept", method = RequestMethod.POST)
	@ApiOperation("新增租户部门")
	public Response createComDepartment(@RequestBody RNode rNode) {
		try {
			Neo4jUtil.createComDepartment(mapper, session, rNode);
		} catch (Exception e) {
			return Response.failed(e.getMessage());
		}
		return Response.success("创建成功");
	}

	@RequestMapping(value = "/create/comUser", method = RequestMethod.POST)
	@ApiOperation("新增租户部门用户")
	public Response createComUser(@RequestBody RNode rNode) {
		try {
			Neo4jUtil.createComUser(mapper, session, rNode);
		} catch (Exception e) {
			return Response.failed(e.getMessage());
		}
		return Response.success("创建成功");
	}

	@RequestMapping(value = "/create/platDept", method = RequestMethod.POST)
	@ApiOperation("新增平台部门")
	public Response createPlatDept(@RequestBody RNode rNode) {
		try {
			Neo4jUtil.createPlatDept(mapper, session, rNode);
		} catch (Exception e) {
			return Response.failed(e.getMessage());
		}
		return Response.success("创建成功");
	}

	@RequestMapping(value = "/create/platUser", method = RequestMethod.POST)
	@ApiOperation("新增平台部门用户")
	public Response createPlatUser(@RequestBody RNode rNode) {
		try {
			Neo4jUtil.createPlatUser(mapper, session, rNode);
		} catch (Exception e) {
			return Response.failed(e.getMessage());
		}
		return Response.success("创建成功");
	}

	@RequestMapping(value = "/create/storeDept", method = RequestMethod.POST)
	@ApiOperation("新增店铺部门")
	public Response createStoreDept(@RequestBody RNode rNode) {
		try {
			Neo4jUtil.createStoreDept(mapper, session, rNode);
		} catch (Exception e) {
			return Response.failed(e.getMessage());
		}
		return Response.success("创建成功");
	}

	@RequestMapping(value = "/create/storeUser", method = RequestMethod.POST)
	@ApiOperation("新增店铺部门用户")
	public Response createStoreUser(@RequestBody RNode rNode) {
		try {
			Neo4jUtil.createStoreUser(mapper, session, rNode);
		} catch (Exception e) {
			return Response.failed(e.getMessage());
		}
		return Response.success("创建成功");
	}

	@RequestMapping(value = "/create/comOrg", method = RequestMethod.POST)
	@ApiOperation("创建租户供应商")
	public Response createComOrg(@RequestBody RNode rNode) {
		try {
			Neo4jUtil.createComOrg(mapper, session, rNode);
		} catch (Exception e) {
			return Response.failed(e.getMessage());
		}
		return Response.success("创建成功");
	}


	@RequestMapping(value = "/create/organization", method = RequestMethod.POST)
	@ApiOperation("新增组织架构")
	public Response createOrganization(@RequestBody RNode rNode) {
		try {

			Neo4jUtil.createOrganization(mapper, session, rNode);

		} catch (Exception e) {
			return Response.failed(e.getMessage());

		}

		return Response.success("创建成功");

	}

	@RequestMapping(value = "/create/role", method = RequestMethod.POST)
	@ApiOperation("新增角色")
	public Response createRole(@RequestBody RNode rNode) {
		try {

			Neo4jUtil.createRole(mapper, session, rNode);

		} catch (Exception e) {
			return Response.failed(e.getMessage());

		}


		return Response.success("创建成功");

	}

	@RequestMapping(value = "/create/permission", method = RequestMethod.POST)
	@ApiOperation("新增权限")
	public Response createPermission(@RequestBody RNode rNode) {
		try {

			Neo4jUtil.createPermission(mapper, session, rNode);

		} catch (Exception e) {
			return Response.failed(e.getMessage());

		}

		return Response.success("创建成功");

	}

	@RequestMapping(value = "/create/userarea", method = RequestMethod.POST)
	@ApiOperation("创建用户区")
	public Response createUserArea(@RequestBody RNode rNode) {
		try {

			Neo4jUtil.createUserArea(mapper, session, rNode);

		} catch (Exception e) {
			return Response.failed(e.getMessage());

		}

		return Response.success("创建成功");

	}

	@RequestMapping(value = "/create/user", method = RequestMethod.POST)
	@ApiOperation("创建用户")
	public Response createUser(@RequestBody RNode rNode) {
		try {

			Neo4jUtil.createUser(mapper, session, rNode);

		} catch (Exception e) {
			return Response.failed(e.getMessage());

		}

		return Response.success("创建成功");

	}

	@RequestMapping(value = "/create/staff", method = RequestMethod.POST)
	@ApiOperation("创建员工")
	public Response createStaff(@RequestBody RNode rNode) {
		try {

			Neo4jUtil.createStaff(mapper, session, rNode);

		} catch (Exception e) {
			return Response.failed(e.getMessage());

		}

		return Response.success("创建成功");

	}

	@RequestMapping(value = "/create/staffRole", method = RequestMethod.POST)
	@ApiOperation("创建员工角色")
	public Response createstaffRole(@RequestBody RNode rNode) {
		try {

			Neo4jUtil.createstaffRole(mapper, session, "", "");

		} catch (Exception e) {
			return Response.failed(e.getMessage());

		}

		return Response.success("创建成功");

	}

	@RequestMapping(value = "/create/cooperationOrg", method = RequestMethod.POST)
	@ApiOperation("创建合作机构")
	public Response createCooperationOrg(@RequestBody RNode rNode) {
		try {

			Neo4jUtil.createStaff(mapper, session, rNode);

		} catch (Exception e) {
			return Response.failed(e.getMessage());

		}

		return Response.success("创建成功");

	}

	/**
	 * 创建节点
	 */
	@RequestMapping(value = "/create/node", method = RequestMethod.POST)
	@ApiOperation("新增节点")
	public Response createNode(@RequestBody RNode rNode) {

		try {

			Neo4jUtil.createNode(mapper, session, rNode);

		} catch (Exception e) {
			return Response.failed(e.getMessage());

		}
		return Response.success("创建成功");

	}

	/**
	 * 创建关系
	 */
	//@RequestMapping(value = "/create/relation", method = RequestMethod.POST)
	//@ApiOperation("创建关系")
	//public Response createRelation(@RequestBody REdge redge, @RequestParam("parId") String parId,
	//                               @RequestParam("childId") String childId) {
	//
	//    try {
	//
	//        RNode srcNode = new RNode();
	//        srcNode.setUuid(Long.parseLong(parId));
	//        srcNode.setEdge(redge);
	//
	//        RNode tarNode = new RNode();
	//        tarNode.setUuid(Long.parseLong(childId));
	//
	//        Neo4jUtil.createRelation(mapper, session, srcNode, tarNode);
	//
	//    } catch (Exception e) {
	//        return Response.failed(e.getMessage());
	//
	//    }
	//    return Response.success("成功");
	//
	//}

	/**
	 * 查询节点
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/query/node", method = RequestMethod.POST)
	@ApiOperation("查询节点")
	public Response queryNode(@RequestParam(value = "uuid", defaultValue = "0") String uuid) {

		RNode rNode = null;
		try {

			rNode = new RNode();
			rNode.setUuid(Long.parseLong(uuid));
			rNode.setLabel("大中台");
			rNode = Neo4jUtil.queryNode(mapper, session, rNode);
			System.out.println(rNode);

		} catch (Exception e) {
			return Response.failed(e.getMessage());
		}

		return Response.data(rNode);
	}

	/**
	 * 查询关系
	 *
	 * @param
	 * @return
	 */
	//@RequestMapping(value = "/query/relation", method = RequestMethod.POST)
	//@ApiOperation("查询关系")
	//public Response queryRelation(@RequestParam("parId") String parId, @RequestParam("childId") String childId,
	//                              @RequestParam("edgeName") String edgeName) {
	//    REdge edge = null;
	//    try {
	//
	//        RNode srcNode = new RNode();
	//        srcNode.setUuid(Long.parseLong(parId));
	//
	//        edge = new REdge();
	//        edge.setName(edgeName);
	//        srcNode.setEdge(edge);
	//
	//        RNode tarNode = new RNode();
	//        tarNode.setUuid(Long.parseLong(childId));
	//
	//        Neo4jUtil.queryRelation(mapper, session, srcNode, tarNode);
	//
	//    } catch (Exception e) {
	//        return Response.failed(e.getMessage());
	//    }
	//
	//    return Response.data(edge);
	//}
}
